package com.pkk.penguin.frame.know.java.driven.ext.beanfactorypostprocess.handle;

import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

/**
 * 自定义bean工厂后置处理器
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-10-30 17:55
 **/
@Slf4j
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor, PriorityOrdered, Ordered {

  /**
   * Modify the application context's internal bean factory after its standard
   * initialization. All bean definitions will have been loaded, but no beans
   * will have been instantiated yet. This allows for overriding or adding
   * properties even to eager-initializing beans.
   *
   * @param beanFactory the bean factory used by the application context
   * @throws BeansException in case of errors
   */
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final int beanDefinitionCount = beanFactory.getBeanDefinitionCount();
    final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
    log.info("【bean工厂后置处理器】-当前BeanFactory有[{}]个bean,bean的信息为:[{}],{}", beanDefinitionCount,
        Arrays.asList(beanDefinitionNames), "  *******************");

  }

  /**
   * Get the order value of this object.
   * <p>Higher values are interpreted as lower priority. As a consequence,
   * the object with the lowest value has the highest priority (somewhat
   * analogous to Servlet {@code load-on-startup} values).
   * <p>Same order values will result in arbitrary sort positions for the
   * affected objects.
   *
   * @return the order value
   * @see #HIGHEST_PRECEDENCE
   * @see #LOWEST_PRECEDENCE
   */
  @Override
  public int getOrder() {
    return HIGHEST_PRECEDENCE - 1000;
  }
}
